﻿using System;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class StumbledLoans : BasePage<StumbledLoansViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {
                storeStumbledLoans.DataSource = this.ViewModel.GetStumbledLoans();
                storeStumbledLoans.DataBind();
            }
        }

        /// <summary>
        /// Handles the Command event of the StumbledLoans control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void StumbledLoans_Command(object sender, AjaxEventArgs e)
        {
            if (e.ExtraParams["Command"] != null)
            {
                switch (e.ExtraParams["Command"].ToString())
                {
                    case "Prosecute":
                        this.RedirectFromThisPage("ClientProsecution", new string[] { "LoanID=" + e.ExtraParams["LoanID"].ToString() });
                        break;
                    case "Reschedule":
                        this.RedirectFromThisPage("LoanRescheduling", new string[] { "LoanID=" + e.ExtraParams["LoanID"].ToString() });
                        break;
                    case "Exempt":
                        wndPenaltyFree.Show();
                        double allDebts = this.ViewModel.GetLoanDebts((e.ExtraParams["LoanID"]).ToGuid());
                        double penalties = this.ViewModel.GetPenalties((e.ExtraParams["LoanID"]).ToGuid());
                        txtAllDebts.Text = allDebts.ToString();
                        txtPenalties.Text = penalties.ToString();
                        txtNumPaymentAmount.MinValue = allDebts - penalties;
                        txtNumPaymentAmount.MaxValue = allDebts - penalties;
                        break;
                    case "CancelDebts":
                        if (!e.ExtraParams["LoanID"].IsNullOrEmpty())
                            this.RedirectFromThisPage("CancelLoanDebts", new string[] { "LoanID=" + e.ExtraParams["LoanID"].ToString() });
                        break;
                    default:
                        this.RedirectToPreviousPage();
                        break;
                }

            }
        }

        /// <summary>
        /// Handles the Clicked event of the BtnFreePenalties control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnFreePenalties_Clicked(object sender, AjaxEventArgs e)
        {
            RowSelectionModel rowSelected = gridStumbledLoans.SelectionModel.Primary as RowSelectionModel;
            this.ViewModel.PayForLoan(rowSelected.SelectedRow.RecordID.ToGuid(), Convert.ToDouble(txtNumPaymentAmount.Value));
            this.RedirectFromThisPage("StumbledLoans");
        }

        public string PermissionName
        {
            get { return Permissions.StumbledLoans.ToString(); }
        }
    }
}
